/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client.user;

import com.smartgwt.client.types.DateDisplayFormat;
import com.smartgwt.client.types.EnterKeyEditAction;
import com.smartgwt.client.types.ListGridEditEvent;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.RowEndEditAction;
import com.smartgwt.client.widgets.form.validator.CustomValidator;
import com.smartgwt.client.widgets.form.validator.Validator;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;

public class TemplateGrid extends ListGrid
{

	public final static int TemplateGridWidth=525;
	
	public TemplateGrid()
	{
		setWidth(TemplateGridWidth);
		setHeight100();
		//setAlternateRecordStyles(true);
		setShowAllRecords(true);
		setShowEmptyMessage(true);
		setEmptyMessage("No Program is available");
		setDateFormatter(DateDisplayFormat.TOUSSHORTDATETIME);
		setOverflow(Overflow.HIDDEN);
		
		//editing
		setCanEdit(true);  
		setEditEvent(ListGridEditEvent.DOUBLECLICK);  
		setListEndEditAction(RowEndEditAction.DONE);  
		setModalEditing(true); 
		setAutoSaveEdits(true);
		setEnterKeyEditAction(EnterKeyEditAction.NEXTCELL);
		setRowEndEditAction(RowEndEditAction.DONE);
		
		
		setDataSource(new TemplateDataSource());

		ListGridField idField = new ListGridField("id", "ID");
		idField.setType(ListGridFieldType.INTEGER);
		idField.setHidden(true);
		idField.setCanEdit(false);
		
		ListGridField nameField = new ListGridField("name", "Name", 60);
		nameField.setType(ListGridFieldType.TEXT);
		Validator validator=new CustomValidator()
		{

			@Override
			protected boolean condition(Object value)
			{
				String str=value.toString();
				if (str.length()<4)
				{
					setErrorMessage("must have 6 characters at minimal");
					return false;
				}
				if (!str.matches("^[a-zA-Z].+"))
				{
					setErrorMessage("First character has to be a letter");
					return false;
				}
				if (!str.matches("(\\w|\\d)+"))
				{
					setErrorMessage("all characters have to be letter or number");
					return false;
				}
				return true;
			}
		};
		nameField.setValidators(validator);
		nameField.setRequired(true);
		
		ListGridField appField = new ListGridField("programName", "App Name", 60);
		appField.setType(ListGridFieldType.TEXT);
		appField.setCanEdit(false);
		
		ListGridField savedTimeField = new ListGridField("savedTime", "Creation Time", 100);
		savedTimeField.setType(ListGridFieldType.DATE);
		savedTimeField.setDateFormatter(DateDisplayFormat.TOUSSHORTDATETIME);
		savedTimeField.setCanEdit(false);

		ListGridField descField = new ListGridField("description", "Description", 300);
		descField.setType(ListGridFieldType.TEXT);

		
		setFields( idField,nameField, appField, savedTimeField,descField);
	}

	boolean loaded = false;

	public void load()
	{
		if (loaded)
			return;
		fetchData();
		loaded = true;
	}

	public void reload()
	{
		invalidateCache();
		fetchData();
		loaded = true;
	}

}
